import axios from 'axios'

//创建request对象
const request = axios.create({
    baseURL: 'http://localhost:8080/user/login', //后台端口
    timeout: 5000
})

//request拦截器
request.interceptors.request.use(config => {
    config.headers['Content-Type'] = 'application/json;charset=utf-8';
    return config
},
error => {
    return Promise.reject(error)
});

//response拦截器，在接口响应后统一处理结果
request.interceptors.response.use(
    response =>{
        let res = response.data; //response.data是为后端返回的Result

        //兼容服务端返回的字符串数据
        if(typeof res === 'string'){
            res = res ? JSON.parse(res) : res
        }
        return res;
    },
    error =>{
        console.log('err' + error)
        return Promise.reject(error)
    }
)

export default request